package web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import objects.Board;
import objects.Player;
import utils.FactoryUI;

/**
 * Servlet implementation class CardsServlet
 */
@WebServlet(name = "Cards", urlPatterns =
{
    "/Cards"
})
public class CardsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CardsServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("application/json");
	    response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.flush();
		resultJson rj = new resultJson();
		
		String playerName = request.getParameter("playerName");
		
		Player player = Board.getInstance().getPlayerByName(playerName);
		
		rj.roomsCards = FactoryUI.getListHtml(player.getRooms());
		rj.weaponsCards = FactoryUI.getListHtml(player.getWeapons());
		rj.charactersCards = FactoryUI.getListHtml(player.getCharacters());
		
		out.print(new Gson().toJson(rj));
	}

	@SuppressWarnings("unused")
	private class resultJson
	{
		String weaponsCards;
		String charactersCards;
		String roomsCards;
	}
}
